<template>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="昵称" prop="ownerNickname">
    <el-input type="text" v-model="ruleForm.ownerNickname" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="头像" prop="ownerHeadpic">
    <el-input type="text" v-model="ruleForm.ownerHeadpic" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="真实姓名" prop="ownerName">
    <el-input type="text" v-model="ruleForm.ownerName" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="手机号" prop="ownerScore">
    <el-input type="text" v-model="ruleForm.ownerScore" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="区域" prop="ownerRegion">
    <el-input type="text" v-model="ruleForm.ownerRegion" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item label="送货地址" prop="ownerAddress">
    <el-input type="text" v-model="ruleForm.ownerAddress" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
    <el-button @click="resetForm('ruleForm')">返回</el-button>
  </el-form-item>
</el-form>
</template>

<script>
import {addOwner} from '@/services/PetService.js'
export default {
    data() {
      var checkTel = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('电话不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value.toString().length !== 11 ) {
              callback(new Error('电话号码必须是11位'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
     
      var validateUserName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入姓名'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          ownerNickname: '',
          ownerName: '',
          ownerHeadpic: '',
          ownerAddress:'',
          ownerRegion:'',
          ownerScore:'',
        },
        rules: {
          // ownerNickname: [
          //   { validator: validatePass, trigger: 'blur' }
          // ],
          ownerName: [
            { validator: validateUserName, trigger: 'blur' }
          ],
          // ownerHeadpic: [
          //   { validator: checkTel, trigger: 'blur' }
          // ],
          // ownerRegion: [
          //   { validator: checkTel, trigger: 'blur' }
          // ],
          ownerScore: [
            { validator: checkTel, trigger: 'blur' }
          ],
          // ownerAddress:[{validator: checkEmail, trigger:'blur'}]
        }
      };
    },
    methods: {
       submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            addOwner(this.ruleForm).then((resp) => {
              console.log(resp.data)
              if(resp){
                this.$router.push({name:"PetOwners"});
              }else{
                alert("服务器正忙，添加失败");
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$router.push({name:"PetOwners"});
      },
    }
  }

</script>

<style>

</style>